package com.wyt.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wyt.dao.BookMapper;
import com.wyt.dao.OrderMapper;
import com.wyt.until.PageEntity;
import com.wyt.until.TableLayui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AAA
 * @date 2021/10/29 18:42("/tee
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private BookMapper bookMapper;

    //    订单审核查询
    @ResponseBody
    @RequestMapping("/selectTeacher")
    public TableLayui selectBook(PageEntity pageEntity,@RequestParam Map map) {
        PageHelper.startPage(pageEntity.getPage(), pageEntity.getLimit());

        List<Map> list= orderMapper.selectOrder(map);
        System.out.println(list+"111111111111111111111111111111111111");
        PageInfo pageInfo=new PageInfo(list);

        return new TableLayui(pageInfo.getTotal(),pageInfo.getList());
    }
    //订单历史查询
    @ResponseBody
    @RequestMapping("/selectTeacher2")
    public TableLayui selectBook2(PageEntity pageEntity,@RequestParam Map map) {
        PageHelper.startPage(pageEntity.getPage(), pageEntity.getLimit());
        List<Map> list= orderMapper.selectOrder2(map);
        PageInfo pageInfo=new PageInfo(list);
        return new TableLayui(pageInfo.getTotal(),pageInfo.getList());
    }

    //根据pid判断增加还是修改
    @ResponseBody
    @RequestMapping("/insertTeacher")
    public TableLayui insertBook1(@RequestParam Map map){
        System.out.println(map);
        if (map.get("pid")!=null&&map.get("pid")!=""){
            orderMapper.updateOrder(map);
            return new TableLayui(1,"失败",map);
        }
        orderMapper.insertOrder(map);
        return new TableLayui(1,"失败",map);
    }

    @ResponseBody
    @RequestMapping(value = "/updateTeacher")
    public TableLayui updateBook(@RequestParam Map map){
        orderMapper.updateOrder(map);
        return new TableLayui(1,"失败",map);
    }

    //删除
    @ResponseBody
    @RequestMapping("/deleteTeacher")
    public TableLayui deleteBook(@RequestParam Map map){
        orderMapper.deleteOrder(map);
        return new TableLayui(1,"失败");
    }


    //修改订单状态  修改t_book的书籍数量
    @ResponseBody
    @RequestMapping("/updateOrderSta")
    public TableLayui updateOrderSta(@RequestParam Map map){
        orderMapper.updateOrderSta(map);

        String  count =  map.get("countt").toString();  //获取发货数量
         int coun=Integer.parseInt(count);
        String bid = map.get("pid").toString();     //获取订单id


        List<Map> list = bookMapper.selectBook2(bid);   //三表联查 找 t_book的id
        Map maps = list.get(0);
        int pid = (int) maps.get("bookid");   // 查询出t_book的id
        int i = (int) maps.get("bookcount");  //  获得t_book中的数量
        int j=i-coun;    //数量减少

        Map map2=new HashMap();
        map2.put("bookcount",j);
        map2.put("bid",pid);
        bookMapper.updateBook3(map2);  //修改t_book的书籍数量
        return new TableLayui(1,"失败");
    }
}
